#include "..\Global.h"

void ServerHeaderHandler::HandlePing(PacketReader &packet) {
	PacketWriter pw;
	if (ns->GetMapleServerType() == 8) {
		if (ns->GetMapleServerVersion() >= 86) {
			pw.Write<uint16_t>(0x1A);
		} else if (ns->GetMapleServerVersion() >= 75) {
			pw.Write<uint16_t>(0x19);
		} else {
			pw.Write<uint16_t>(0x18);
		}
	}
	ns->SendPacket(pw);
}

void ServerHeaderHandler::HandleLogonResult(PacketReader &packet) {
	short answerType = packet.Read<short>();
	if (answerType == 0) {
		string username;
		int userid;
		bool admin;
		long qban, createtime;
		int8_t state, qbanreason;
		if (ns->GetMapleServerVersion() == 75) {
			packet.Skip(4);
			userid = packet.Read<int>();
			state = packet.Read<int8_t>();
			admin = packet.Read<int8_t>();
			packet.Skip(2);
			username = packet.ReadString();
			packet.Skip(1);
			qbanreason = packet.Read<int8_t>();
			qban = packet.Read<long>();
			createtime = packet.Read<long>();
			packet.Skip(4);
		}
		else {
			cout << "Couldn't handle login packet!" << endl;
			return;
		}
		cout << "Logged in as " << username << " with userid " << userid << ". Admin? " << (admin ? "Yes!" : "Nope.") << " Gender: " << (state == 1 ? "Female" : "Male") << endl;
		if (state <= 1) {
			PacketWriter pw(0x09);
			pw.Write<long>(0x01);
			pw.WriteString("");
			ns->SendPacket(pw);
		}
	}
}

void ServerHeaderHandler::HandlePINResult(PacketReader &packet) {
	int8_t opcode = packet.Read<int8_t>();
	if (opcode == 0x00) {
		Map::ChangeLoginStep(1);
		PacketWriter pw(0x0B);
		ns->SendPacket(pw);
	}
}